import re
import sys

from pyquery import PyQuery as pq

from commen.tools.__http import *
from commen.tools.__db import *

def parse_data(result, html):
    '''
    create table t_goods (
    id int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,PRIMARY KEY(`id`),
    titles VARCHAR(255) NOT NULL DEFAULT '',
    prices VARCHAR(255) NOT NULL DEFAULT '',
    deals VARCHAR(255) NOT NULL DEFAULT '',
    pics VARCHAR(255) NOT NULL DEFAULT '',
    detailurls VARCHAR(255) NOT NULL DEFAULT '',
    locations VARCHAR(255) NOT NULL DEFAULT '',
    shops VARCHAR(255) NOT NULL DEFAULT '',
    shopurls VARCHAR(255) NOT NULL DEFAULT '') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
    从html中提取每个信息，并格式化为字典，存储在result这个列表中的同时，保存到数据库中去
    :param result: 结果列表
    :param html: html源码
    '''
    try:
        titles = re.findall(r'\"raw_title\":\".*?\"', html)
        prices = re.findall(r'\"view_price\":\"[\d.]*\"', html)
        deals = re.findall(
            r'\"view_sales\":\"[0-9]*[1-9][0-9]*[\u4e00-\u9fa5]+\"', html)
        pics = re.findall(r'\"pic_url\":\".*?"', html)
        detailurls = re.findall(r'\"detail_url":\".*?\"', html)
        locations = re.findall(r'\"item_loc\":\"\D*?\"', html)
        shops = re.findall(r'\"nick\":\".*?\"', html)
        shopurls = re.findall(r'\"shopLink\":\".*?\"', html)
        for i in range(len(titles)):
            product = [
                eval(str(titles[i].split(":")[1])),
            eval(str(prices[i].split(":")[1])),
            eval(str(deals[i].split(":")[1]))[:-3],
            "https:" + eval(str(pics[i].split(":")[1])),
            ("https:" + eval(str(detailurls[i].split(":")[1]))).encode("unicode_escape").decode("unicode_escape"),
            eval(str(locations[i].split(":")[1])),
            eval(str(shops[i].split(":")[1])),
            "https:" + eval(str(shopurls[i].split(":")[1])).encode("unicode_escape").decode("unicode_escape")
            ]
            # product = {
            #     'title':
            #         eval(str(titles[i].split(":")[1])),
            #     'price':
            #         eval(str(prices[i].split(":")[1])),
            #     'deal':
            #         eval(str(deals[i].split(":")[1]))[:-3],
            #     'pic':
            #         "https:" + eval(str(pics[i].split(":")[1])),
            #     'detailurl':
            #         ("https:" + eval(str(detailurls[i].split(":")[1]))
            #          ).encode("unicode_escape").decode("unicode_escape"),
            #     'location':
            #         eval(str(locations[i].split(":")[1])),
            #     'shop':
            #         eval(str(shops[i].split(":")[1])),
            #     'shopurl':
            #         "https:" + eval(str(shopurls[i].split(":")[1])).encode(
            #             "unicode_escape").decode("unicode_escape")
            # }
            # print(product)
            result.append(product)
    except Exception as e:
        print(str(e))

def getUrl(keyword, page):
    url = "https://s.taobao.com/search?q=" + keyword + "&sort=sale-desc&s=" + str(page)
    return url

def printInfo(selects):
    if selects:
        for s in selects:
            print(str(s))
    else:
        print("is Null")

if __name__ == "__main__":
    print("************************ execute main")
    # sys.argv ##获取键盘输入内容
    param = sys.argv
    results = []
    for i in range(1):
        print("第" + str(i) + "轮爬取数据!!!开始")
        htmlText = get_html(getUrl("零食", 22 * i))
        pqsource = pq(htmlText)
        parse_data(results, htmlText)
        print("第" + str(i) + "轮爬取数据!!!结束")
    db = DBHelper("taobaotest")
    for result in results:
        content = "'%s','%s','%s','%s','%s','%s','%s','%s'" % tuple(result)
        db.db_insert_norepeat("t_goods",
                              "titles,prices,deals,pics,detailurls,locations,shops,shopurls", content
                              , "titles", " WHERE titles='" + result[0] + "'"
                              )
    # db.db_insert()
    # db.db_update("tbl_actor", "nickname='zeyzeyzey'")
    # db.db_update("tbl_actor", "nickname='sdfg'", "id=%s", 9088)
    # db.db_insert("tbl_actor", "(id,uid, nickname)values(%s,%s,%s)", (33, str(24), '【三只松鼠_猪肉猪肉脯210g】休闲食品小吃零食靖江特产猪肉干'))
    # printInfo(db.db_select("tbl_actor"))
    # db.db_insert("tbl_actor", "(id,uid,nickname)values(%s,%s,%s)",
    #              [(2634573, str(4433), "sadfa"), (536322, str(2456347), "ergerg")])
    # db.db_show(db.DBSHOW_TABLES, "taobaotest")
    printInfo(db.db_select("t_goods"))
    # db.db_delete("t_goods")